﻿using System;
using System.Data;
using System.Data.Common;
using System.Configuration;

namespace Db
{
    public class DBTrans : IDisposable
    {

        /// <summary>
        /// 执行事物
        /// </summary>
        /// <param name="doFunc"></param>
        public static void DoTrans(Action<DbHelper, yq.CaCheRefresh> doFunc)
        {
            using (DBTrans ts = new DBTrans())
            {
                DbHelper helper = new DbHelper(ts);
                try
                {
                    doFunc(helper, helper.CaCheRefresh);

                    //防止未提交事物引起的异常
                    if (helper.TransType== TransType.Open)
                    {
                        helper.Commit();
                    }
                }
                catch
                {
                    helper.Rollback();
                    helper.CaCheRefresh.DoTry();
                    throw;
                }
            }
        }

        /// <summary>
        /// 执行事物
        /// </summary>
        /// <typeparam name="F"></typeparam>
        /// <param name="dofunc"></param>
        /// <returns></returns>
        public static F DoTrans<F>(Func<DbHelper, yq.CaCheRefresh, F> doFunc)
        {
            using (DBTrans ts = new DBTrans())
            {
                DbHelper helper = new DbHelper(ts);
                try
                {
                    var outval = doFunc(helper, helper.CaCheRefresh);

                    //防止未提交事物引起的异常
                    if (helper.TransType == TransType.Open)
                    {
                        helper.Commit();
                    }

                    return outval;
                }
                catch
                {
                    helper.Rollback();
                    helper.CaCheRefresh.DoTry();
                    throw;
                }
            }

        }

        private DbConnection conn;

        private DbTransaction dbTrans;

        /// <summary>
        /// 链接
        /// </summary>
        public DbConnection DbConnection
        {
            get { return this.conn; }
        }

        /// <summary>
        /// 事物
        /// </summary>
        public DbTransaction DbTrans
        {
            get { return this.dbTrans; }
        }

        public DBTrans()
        {
            conn = DbHelper.CreateConnection();
            conn.Open();
            dbTrans = conn.BeginTransaction();
        }
        public DBTrans(string connectionString)
        {
            conn = DbHelper.CreateConnection(connectionString);
            conn.Open();
            dbTrans = conn.BeginTransaction();
        }

        public void Dispose()
        {
            this.Colse();
        }

        public void Colse()
        {
            if (conn.State == System.Data.ConnectionState.Open)
            {
                conn.Close();
            }
        }
    }
}
